package com.example.book.api;

import com.example.book.base.ApiBaseController;
import com.example.book.base.Result;
import com.example.book.common.annotation.LoginIgnore;
import com.example.book.in.entity.BookCatalog;
import com.example.book.in.entity.BookCategory;
import com.example.book.in.service.BookCatalogService;
import com.example.book.in.service.BookCategoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/book")
public class ApiBookCategoryController extends ApiBaseController {

    private BookCategoryService categoryService;

    private ApiBookCategoryController(BookCategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 获取书籍分类
     *
     * @return
     */
    @GetMapping("/category")
    @LoginIgnore
    public Result getCategory() {

        List<BookCategory> categories = categoryService.findList(new BookCategory());

        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> map = null;
        for (BookCategory bc : categories) {
            if (bc.getStatus().equals("0")) {
                map = new HashMap<>();
                map.put("name", bc.getName());
                map.put("id", bc.getId());
                list.add(map);
            }
        }

        return Result.success(list);
    }

}
